package com.luobo.yfh.vo.request;

import com.alibaba.fastjson.JSON;
import com.luobo.yfh.vo.AesUtil;
import com.luobo.yfh.vo.MD5Util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@ApiModel(value = "请求入参")
public class RequestVo<T> implements Serializable {

    @Valid
    @NotNull(message = "请求头不可为null")
    private RequestVo.Head head;
    @Valid
    @NotNull(message = "请求体不可为null")
    private RequestVo.Body<T> body;

    public Head getHead() {
        return head;
    }

    public void setHead(Head head) {
        this.head = head;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    public static class Head {
        @NotBlank(message = "接口标识不可为空")
        @ApiModelProperty(value = "接口标识", required = true, example = "orderApply")
        private String cmd;
        @NotBlank(message = "金融机构标识不可为空")
        @ApiModelProperty(value = "金融机构标识", required = true, example = "DaDi")
        private String fcid;
        @Min(value = 1584754823,message = "Unix时间戳")
        @ApiModelProperty(value = "Unix时间戳", required = true, example = "1584754823")
        private Long timestamp;


        public Head() {
        }

        public Head(String cmd, String fcid, Long timestamp) {
            this.cmd = cmd;
            this.fcid = fcid;
            this.timestamp = timestamp;
        }

        public String getCmd() {
            return cmd;
        }

        public void setCmd(String cmd) {
            this.cmd = cmd;
        }

        public String getFcid() {
            return fcid;
        }

        public void setFcid(String fcid) {
            this.fcid = fcid;
        }

        public Long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(Long timestamp) {
            this.timestamp = timestamp;
        }
    }


    public static class Body<T> {
        @NotBlank(message = "请求签名不可为空")
        @ApiModelProperty(value = "请求签名", required = true, example = "生成规则 详见MD5Util")
        private String sign;
        @NotBlank(message = "请求内容不可为空")
        @ApiModelProperty(value = "业务加密数据", required = true, example = "生成规则 详见AesUtil")
        private String content;

        public Body() {
        }

        public Body(String sign, String content) {
            this.sign = sign;
            this.content = content;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
         this.sign = MD5Util.createSign(sign);
        }

        public String getContent() {
            return content;
        }

        public void setContent(T content) {
            this.content = AesUtil.encry(JSON.toJSONString(content));
        }
    }


}
